<?php

/**
 * Clean User Inputs
 * 
 * @param type $str
 * @return type
 */
function sanitize($str = null) {
  return trim(htmlentities(strip_tags($str), ENT_QUOTES, 'UTF-8'));
}

/**
 * Clean Name (Display Name, Place Name, Etc...)
 * 
 * @param type $str
 * @return type
 */
function namefy_text($str = null) {
  return trim(ucwords(preg_replace('/[^a-zA-Z ]/', '', substr(strtolower($str), 0, 50))));
}

/**
 * Clean Number (Telephone, Mobile, Card, Etc...)
 * 
 * @param type $str
 * @return type
 */
function numerify_text($str = null) {
  return trim(preg_replace('/[^0-9]/', '', substr($str, 0, 50)));
}

/**
 * Urlfy a Text
 * 
 * @param type $text1
 * @param type $text2
 * @param type $text3
 * @return type
 */
function urlfy_text($text1 = null, $text2 = null, $text3 = null) {
  return url_title(trim($text1 . ' ' . $text2 . ' ' . $text3), '_', true);
}

/**
 * Get current url including query strings
 * 
 */
function current_full_url($full = false) {
  $ci = & get_instance();
  $url = ($full) ? $ci->config->site_url($ci->uri->uri_string()) : $ci->uri->uri_string();
  return $_SERVER['QUERY_STRING'] ? $url . '?' . $_SERVER['QUERY_STRING'] : $url;
}

/**
 * Add protocol in url
 * 
 */
function add_http($url) {
  if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
    $url = "http://" . $url;
  }
  return $url;
}

/**
 * Profile Image & Thumnail Url
 * 
 * @param type $img_file
 * @param type $thumbnail
 * @return type
 */
function user_image_url($img_file = null, $thumbnail = false) {
  $img_file = ($thumbnail) ? substr($img_file, 0, 32) . '_thumbnail.jpg' : $img_file;
  if (!empty($img_file)) {
    if (file_exists(USER_IMG_FOLDER . $img_file)) {
      return site_url('uploads/user/' . $img_file);
    }
  }
  return site_url('assets/web/img/user_default.jpg');
}

/**
 * Advert Image & Thumbnail Url
 * 
 * @param type $img_file
 * @param type $thumbnail
 * @return type
 */
function advert_image_url($img_file = null, $thumbnail = false) {
  $img_file = ($thumbnail) ? substr($img_file, 0, 32) . '_thumbnail.jpg' : $img_file;
  if (!empty($img_file)) {
    if (file_exists(ADVERT_IMG_FOLDER . $img_file)) {
      return site_url('uploads/advert/' . $img_file);
    }
  }
  return site_url('assets/web/img/advert_default.jpg');
}
